export default class GestureDector {
    private touchStart: cc.Vec2 = cc.v2()
    private touchMove: cc.Vec2 = cc.v2()
    private touchLastMove: cc.Vec2 = cc.v2()
    private touchDelta: cc.Vec2 = cc.v2()
    private target: cc.Node = null
    private moveCallback: (moved: cc.Vec2, currentPos?: cc.Vec2, distance?: cc.Vec2) => void = null

    constructor(node: cc.Node) {
        this.target = node
        this.target.on(cc.Node.EventType.TOUCH_START, function (ev: Event) {
            this.touchStart = this.target.convertTouchToNodeSpace(ev)
            this.touchLastMove.set(this.touchStart)
        }.bind(this))
        this.target.on(cc.Node.EventType.TOUCH_MOVE, function (ev: Event) {
            this.touchMove.set(this.target.convertTouchToNodeSpace(ev))
            this.touchMove.sub(this.touchLastMove, this.touchDelta)
            this.touchLastMove.set(this.touchMove)
            if (this.moveCallback) {
                this.moveCallback(this.touchDelta, this.touchMove, this.touchMove.sub(this.touchStart))
            }
        }.bind(this))
    }

    onMove(callback: (moved: cc.Vec2, currentPos?: cc.Vec2, distance?: cc.Vec2) => void) {
        this.moveCallback = callback
    }

}